<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="创建人ID;老师ID" prop="teacherId">-->
<!--        <el-input-->
<!--          v-model="queryParams.teacherId"-->
<!--          placeholder="请输入创建人ID;老师ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="所属课程" prop="courseId">
        <el-select v-model="queryParams.courseId" placeholder="请选择">
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.courseName"
            :value="item.id">
          </el-option>
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.courseId"-->
<!--          placeholder="请输入所属课程ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item label="题目标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题目标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分值" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入分值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="难度" prop="difficulty">
        <el-input
          v-model="queryParams.difficulty"
          placeholder="请输入难度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teach:question:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['teach:question:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['teach:question:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teach:question:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="创建人ID;老师ID" align="center" prop="teacherId" />-->
<!--      <el-table-column label="所属课程ID" align="center" prop="courseId" />-->
      <el-table-column label="所属课程名称" align="center" prop="courseName" />
      <el-table-column label="题目标题" align="center" prop="title" />
      <el-table-column label="答案" align="center" prop="answer">

      </el-table-column>
      <el-table-column label="选择题选项" align="center" prop="answerOptions"   type="expand"  >
        <template slot-scope="props">
          <div v-for="(item,index) in JSON.parse(props.row.answerOptions)" :key="index">选项{{index + 1}}：{{item.atitle }}</div>
          <div v-if="props.row.answerKeys != null"><stronge>答案解析:</stronge></div>
          <div>{{props.row.answerKeys}}</div>
        </template>
      </el-table-column>
<!--      <el-table-column label="题目设计图片" align="center" prop="answerImages" />-->
<!--      <el-table-column label="答案解析" align="center" prop="answerKeys" />-->
      <el-table-column label="分值" align="center" prop="score" />
      <el-table-column label="试题类型" align="center" prop="type" :formatter="showQuestionType"/>
<!--      <el-table-column label="知识点ID" align="center" prop="knowledgePointId" />-->
      <el-table-column label="知识点" align="center" prop="knowledgePointName" />
      <el-table-column label="难度" align="center" prop="difficulty" />
<!--      <el-table-column label="状态;0->正常 1->删除" align="center" prop="status" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['teach:question:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teach:question:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="创建人ID;老师ID" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入创建人ID;老师ID" />
        </el-form-item>
        <el-form-item label="所属课程ID" prop="courseId">
          <el-input v-model="form.courseId" placeholder="请输入所属课程ID" />
        </el-form-item>
        <el-form-item label="所属课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入所属课程名称" />
        </el-form-item>
        <el-form-item label="题目标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入题目标题" />
        </el-form-item>
        <el-form-item label="答案" prop="answer">
          <el-input v-model="form.answer" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选择题选项" prop="answerOptions">
          <el-input v-model="form.answerOptions" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="题目设计图片" prop="answerImages">
          <el-input v-model="form.answerImages" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="答案解析" prop="answerKeys">
          <el-input v-model="form.answerKeys" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分值" prop="score">
          <el-input v-model="form.score" placeholder="请输入分值" />
        </el-form-item>
        <el-form-item label="知识点ID" prop="knowledgePointId">
          <el-input v-model="form.knowledgePointId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="知识点" prop="knowledgePointName">
          <el-input v-model="form.knowledgePointName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="难度" prop="difficulty">
          <el-input v-model="form.difficulty" placeholder="请输入难度" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuestion, getQuestion, delQuestion, addQuestion, updateQuestion } from "@/api/teach/question";
import { listCourse } from "@/api/teach/course";

export default {
  name: "Question",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题目表格数据
      questionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        courseId: null,
        courseName: null,
        title: null,
        answer: null,
        answerOptions: null,
        answerImages: null,
        answerKeys: null,
        score: null,
        type: null,
        knowledgePointId: null,
        knowledgePointName: null,
        difficulty: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teacherId: [
          { required: true, message: "创建人ID;老师ID不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "所属课程ID不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "题目标题不能为空", trigger: "blur" }
        ],
        score: [
          { required: true, message: "分值不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "试题类：0->选择；1->多选；2->判断；3->填空；4->问答不能为空", trigger: "change" }
        ],
        difficulty: [
          { required: true, message: "难度不能为空", trigger: "blur" }
        ],
      },
      courseList:[]
    };
  },
  created() {
    this.getList();
    this.getCourseList();
  },
  methods: {
    /** 查询课程列表 */
    getCourseList() {
      this.loading = true;
      listCourse({pageNum:1,pageSize:10000}).then(response => {
        this.courseList = response.rows;

        this.loading = false;
      });
    },
    /** 查询题目列表 */
    getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherId: null,
        courseId: null,
        courseName: null,
        title: null,
        answer: null,
        answerOptions: null,
        answerImages: null,
        answerKeys: null,
        score: null,
        type: null,
        knowledgePointId: null,
        knowledgePointName: null,
        difficulty: null,
        status: 0,
        createTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加题目";
      this.$router.push({path:'/teach/addQuestion'});

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuestion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改题目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuestion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除题目编号为"' + ids + '"的数据项？').then(function() {
        return delQuestion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teach/question/export', {
        ...this.queryParams
      }, `question_${new Date().getTime()}.xlsx`)
    },
    // 显示答案选项
    showAnswerOptions(row, column, cellValue, index){
      let ans = JSON.parse(row.answerOptions);
      let str = "";
      console.log(ans);
      let i = 0;
      for(i in ans){
        str += "选项"+(parseInt(i) + 1) +":"+ans[i].atitle;
      }
      return str;
    },
    showQuestionType(row, column, cellValue, index){
      // 0->选择；1->多选；2->判断；3->填空；4->问答不能为空
      let arr = ["选择","多选","判断","填空","问答"];
      return arr[row.type];
    }
  }
};
</script>
